<?php
require_once '../../classes/includeAll_Layer2.php';

$action = filter_input(INPUT_POST, "action");
if($action == null || $action == false)
{
  echo json_encode (array("success"=>"false","message"=>"Ungültige Anweisung"));
  exit();
}
require '../../resources/getConnection.php';
$roomAndLevelClass = new roomAndLevel($connection);
$message = "";
$success = "";
$option = "";
$row = "";
$partly = false;
$deleted = array();

switch ($action) {
  case "delete":
    $target = filter_input(INPUT_POST, "target");
    $id = filter_input(INPUT_POST, "id", FILTER_VALIDATE_INT);
    if($target == null || $target == false || $id == null || $id == false)
    {
      $message = "Ungültige Anweisung";
      $success = "false";
    }
    else
    {
      switch ($target) {
        case "level":
          $rooms = $roomAndLevelClass->getAllRoomsPerLevel($id);
          $roomAndLevelClass->LevelID=$id;
          foreach ($rooms as $room) {
            $roomAndLevelClass->RoomID = $room["ID"];
            $erg = $roomAndLevelClass->DeleteRoom();
            $partly = $partly || $erg;
            if($erg)
              array_push ($deleted, $room["ID"]);
          }
          if($roomAndLevelClass->DeleteLevel())
          {
            $success = "true";
            $partly = "full";
          }
          else
          {
            if($partly)
            {
              $success = "true";
              $partly = "partly";
              $message = "Einige Räume konnten nicht gelöscht werden, da sie verwendet werden.";
            }
            else
            {
              $message = "Alle Räume innerhalb dieses Stockwerks werden verwendet.";
              $success = "false";
              $partly = "none";
            }
          }
          break;
        case "room":
          $roomAndLevelClass->RoomID=$id;
          if($roomAndLevelClass->DeleteRoom())
            $success = "true";
          else
          {
            $success = "false";
            $message = "Raum wird verwendet.";
          }
          break;

        default:
          $message = "Ungültige Anweisung";
          $success = "false";
          break;
      }
    }

    break;
    
    case "add":
      
      $mode = filter_input(INPUT_POST, "mode");
      if($mode == null || $mode == false)
      {
        $message = "Ungültige Anweisung";
        $success = "false";
      }
      else
      {
        switch ($mode)
        {
          case "level":
            $name = filter_input(INPUT_POST, "levelname");
            if($name == null || $name == false || $name == "" )
            {
              $message = "Ungültige Anweisung";
              $success = "false";
            }
            else
            {
              $roomAndLevelClass->LevelName=$name;
              if($roomAndLevelClass->LevelExistsOrCreate())
              {
                $success = "true";
                $row = '<tr class="level">'
                  . '<td name="entry['.$roomAndLevelClass->LevelID.'][0]" info="levelName">'.$roomAndLevelClass->LevelName.'</td>'
                  . '<td></td>'
                  . '<td></td>'
                  . '<td objid="'.$roomAndLevelClass->LevelID.'"><img src="../design/pictures/delete.png" width="20" alt="X"/></td>'
                 . '</tr>';
                $option = '<option value="'.$roomAndLevelClass->LevelID.'">'.$roomAndLevelClass->LevelName.'</option>';
              }
              else
              {
                $success="false";
                $message="Stockwerk existiert bereits.";
              }
            }
            break;
          case "room":
            $name = filter_input(INPUT_POST, "roomname");
            $number = filter_input(INPUT_POST, "roomnumber");
            $levelID = filter_input(INPUT_POST, "levelselect",FILTER_VALIDATE_INT);
            if($name == null || $name == false || $number == null || $number == false || $levelID == null || $levelID == false || $levelID <0)
            {
              $success = "false";
              $message = "Ungültige Anweisung bei Argumenten\nName: $name\n Number: $number\nLevel: $levelID";
            }
            else
            {
              $roomAndLevelClass->RoomName = $name;
              $roomAndLevelClass->RoomNumber = $number;
              $roomAndLevelClass->LevelID = $levelID;
              if($roomAndLevelClass->RoomExistsOrCreate(false))
              {
                $success = "true";
                $row ='<tr class="room">'
              . '<td name="entry['.$roomAndLevelClass->RoomID.'][1]" info="level"></td>'
              . '<td name="entry['.$roomAndLevelClass->RoomID.'][2]" info="roomNumber">'.$number.'</td>'
              . '<td name="entry['.$roomAndLevelClass->RoomID.'][3]" info="roomName">'.$name.'</td>'
              . '<td objid="'.$roomAndLevelClass->RoomID.'"><img src="../design/pictures/delete.png" width="20" alt="X"/></td>'
             . '</tr>';
                $option = $levelID;
              }
              else 
              {
                $success = "false";
                $message = "Raum existiert bereits";
              }
            }
            break;

          default :
            $message = "Ungültige Anweisung";
            $success = "false";
            break;
        }
      }
      break;

  case "update":
    if(isset($_POST["entry"]))
    {
      $row = array();
      $entries=$_POST["entry"];
      foreach ($entries as $id => $val) {
        $levelChanged = false;
        $values = array();
        $names=array();
        foreach ($val as $key => $change) {
          if($key==0)
          {
            $roomAndLevelClass->LevelID=$id;
            $roomAndLevelClass->UpdateLevel (array($change), array('name'));
            $singleRow= '<tr class="level">'
                  . '<td name="entry['.$roomAndLevelClass->LevelID.'][0]" info="levelName">'.$change.'</td>'
                  . '<td></td>'
                  . '<td></td>'
                  . '<td objid="'.$roomAndLevelClass->LevelID.'"><img src="../design/pictures/delete.png" width="20" alt="X"/></td>'
                 . '</tr>';
            $option = '<option value="'.$roomAndLevelClass->LevelID.'">'.$roomAndLevelClass->LevelName.'</option>';
            array_push($row, array("type"=>"level","level"=>$id,"row"=>$singleRow));
          }
          else
          {
            $name = "";
            switch ($key) {
              case 1:
                $levelChanged=true;
                $name="level_ID";
                break;
              case 2:
                $name="number";
                break;
              case 3:
                $name="name";
                break;
              default:
                break;
            }
            if($name!="")
            {     
              array_push($names, $name);
              array_push($values, $change);
            }
          }
        }
        if(count($names)>0)
        {
          $roomAndLevelClass->RoomID=$id;
          $roomAndLevelClass->UpdateRoom($values, $names);
          $roomAndLevelClass->getRoomInfo();
          
          $singleRow ='<tr class="room">'
            . '<td name="entry['.$roomAndLevelClass->RoomID.'][1]" info="level"></td>'
            . '<td name="entry['.$roomAndLevelClass->RoomID.'][2]" info="roomNumber">'.$roomAndLevelClass->RoomNumber.'</td>'
            . '<td name="entry['.$roomAndLevelClass->RoomID.'][3]" info="roomName">'.$roomAndLevelClass->RoomName.'</td>'
            . '<td objid="'.$roomAndLevelClass->RoomID.'"><img src="../design/pictures/delete.png" width="20" alt="X"/></td>'
            . '</tr>';
          array_push($row, array("type"=>"room","levelChanged"=>$levelChanged,"room"=>$roomAndLevelClass->RoomID,"level"=>$roomAndLevelClass->LevelID,"row"=>$singleRow));
          
        }
      }
      $success = "true";
    }
    else
    {
      $message="Keine Änderung erkannt.";
      $success="false";
    }
    break;
  default:
    $message = "Ungültige Anweisung";
    $success = "false";
    break;
}
$connection->disconnectDB();
echo json_encode (array("success"=>$success,"message"=>$message,"partly"=>$partly,"deletedRooms"=>$deleted,"row"=>$row,"option"=>$option));
